package net.xumf.support.model;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import net.xumf.support.enums.CodeEnum;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class ResponseResult<T> {

    private boolean success;
    private int code;
    private String msg;
    private String detailMsg;
    private T data;

    public ResponseResult(boolean success, CodeEnum codeEnum, String detailMsg, T data) {
        this.success = success;
        this.code = codeEnum.getCode();
        this.msg = codeEnum.getMsg();
        this.detailMsg = detailMsg;
        this.data = data;
    }

    public static <U> ResponseResult<U> success(CodeEnum codeEnum, String detailMsg, U u) {
        return new ResponseResult<>(true, codeEnum, detailMsg, u);
    }

    public static <U> ResponseResult<U> success(String detailMsg, U u) {
        return success(CodeEnum.SUCCESS, detailMsg, u);
    }

    public static <U> ResponseResult<U> success(U u) {
        return success(CodeEnum.SUCCESS.getMsg(), u);
    }

    public static ResponseResult success(CodeEnum codeEnum) {
        return success(codeEnum, null == codeEnum ? "" : codeEnum.getMsg(), null);
    }

    public static ResponseResult success() {
        return success(CodeEnum.SUCCESS, CodeEnum.SUCCESS.getMsg(), null);
    }

    public static ResponseResult failure(CodeEnum codeEnum, String detailMsg) {
        return new ResponseResult<>(false, codeEnum, detailMsg, null);
    }

    public static ResponseResult failure(int code, String msg) {
        return new ResponseResult<>(false, code, msg, msg, null);
    }

    public static ResponseResult failure(CodeEnum codeEnum) {
        return failure(codeEnum, codeEnum.getMsg());
    }

    public static ResponseResult failure(String detailMsg) {
        return failure(CodeEnum.FAILURE, detailMsg);
    }

    public static ResponseResult failure() {
        return failure(CodeEnum.FAILURE);
    }
}
